package soft.quicknote;

import android.app.DatePickerDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import org.joda.time.LocalDate;


public class QrcodeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);
        findViewById(R.id.qrcode_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doGenrate(v);
                hideInputMethod();
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {

            default:
                return super.onOptionsItemSelected(item);
        }
    }


    public void showDatePickerDialog() {
        LocalDate date = LocalDate.now();
        DatePickerDialog datePickerDialog = new DatePickerDialog(
                QrcodeActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(QrcodeActivity.this.getApplicationContext(), String.format("你输入的日期是: %s年%s月%s日", year, month, dayOfMonth), Toast.LENGTH_LONG).show();
                EditText et =  (EditText) QrcodeActivity.this.findViewById(R.id.date_text);

            }
        }, date.getYear(), date.getMonthOfYear(), date.getDayOfMonth());
        datePickerDialog.show();
    }


    public void doGenrate(View v) {
        EditText text = (EditText) findViewById(R.id.qrcode_data);
        generateQRCode(text.getText().toString());
    }


    private void generateQRCode(String data) {
        com.google.zxing.Writer writer = new QRCodeWriter();
        String finaldata = Uri.encode(data, "ISO-8859-1");
        ImageView mImageView = (ImageView) findViewById(R.id.mBitmap);
        Bitmap mBitmap = Bitmap.createBitmap(350, 350, Bitmap.Config.ARGB_8888);
        try {
            BitMatrix bm = writer.encode(finaldata, BarcodeFormat.QR_CODE, 350, 350);
            mBitmap = Bitmap.createBitmap(350, 350, Bitmap.Config.ARGB_8888);
            for (int i = 0; i < 350; i++) {
                for (int j = 0; j < 350; j++) {
                    mBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
                }
            }
        } catch (WriterException e) {
            e.printStackTrace();
        }
        if (mBitmap != null) {
            mImageView.setImageBitmap(mBitmap);
        }
    }

    private void hideInputMethod() {
        // Check if no view has focus:
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
